package com.van.oauth.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.van.entity.ResPonObject;
import com.van.entity.account.Account;
import com.van.entity.account.User;
import com.van.entity.constant.ResStatus;
import com.van.service.account.UserService;
import com.van.service.sys.RedisService;
import com.van.service.sys.SysLogService;
import com.van.utils.DateUtils;
import com.van.utils.JwtUtil;
import com.van.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.logging.log4j.util.Strings;
import org.jose4j.jwk.RsaJsonWebKey;
import org.jose4j.jwk.RsaJwkGenerator;
import org.jose4j.lang.JoseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.PrivateKey;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author VanKa qq:2395073039
 * @date 2021/8/17 16:02
 */

@Api(tags = "登录")
@ResponseBody
@Controller
@RequestMapping("/login")
public class AccountController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysLogService sysLogService;

    /**
     * 游客账号密码登录
     *
     * @param account  账号
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "/account", method = RequestMethod.POST)
    @ApiOperation(value = "登录", notes = "账号密码登录")
    public ResPonObject getList(HttpServletRequest request,
                                @Parameter(name = "account", description = "账号", required = true) @RequestParam(name = "account") String account,
                                @Parameter(name = "password", description = "密码", required = true) @RequestParam(name = "password") String password)  throws JoseException {
        if (Strings.isEmpty(account)) {
            return new ResPonObject("请输入账号", ResStatus.NOT_LOGIN);
        }
        if (Strings.isEmpty(password)) {
            return new ResPonObject("请输入密码", ResStatus.NOT_LOGIN);
        }
        User u = userService.getOneData(new QueryWrapper<User>().eq("phone", account));

        if(u == null){
            return new ResPonObject("请重新输入账号", ResStatus.NOT_LOGIN);
        }
        //判断状态 0.禁用 1.待审核 2.正常使用
        if (u.getStatus() == 0){
            return new ResPonObject("用户被禁止使用", ResStatus.NOT_LOGIN);
        }else if (u.getStatus() == 1){
            return new ResPonObject("用户账号待激活", ResStatus.NOT_LOGIN);
        }

        System.out.println(MD5Util.MD5Encode(password,"tourist"));
        if (!MD5Util.MD5Encode(password,"tourist").equals(u.getPassword())){
            return new ResPonObject("用户密码错误", ResStatus.NOT_LOGIN);
        }
        Date d1 = new Date();
        RsaJsonWebKey WebKey = RsaJwkGenerator.generateJwk(2048);
        PrivateKey privateKey = WebKey.getPrivateKey();
        String token = JwtUtil.createToken(privateKey, new Account(u));
        System.out.println("生成token，用时：" + DateUtils.timeLagSecond(d1, new Date()));

        if (Strings.isEmpty(token)) {
            return new ResPonObject("用户信息生成失败！", ResStatus.NOT_FIND);
        }
        // redis 保存
        redisService.set(token,WebKey.getKey(),6 * 60 * 60L);

//        sysLogService.addAdminLog(u,"登录平台管理系统", ResStatus.LOGIN);

        u.setLastLoginTime(new Date());
        userService.updateById(u);

        Map<String,Object> params = new HashMap<>();
        params.put("token",token);
        params.put("user",u);
        return new ResPonObject();
    }

}
